package leetcode.code2453;

import java.util.Arrays;

// 超内存
public class Solution {
	public int destroyTargets(int[] nums, int space) {
		int[] cnt = new int[space];
		int[] min = new int[space];
		Arrays.fill(min, Integer.MAX_VALUE);
		for (int i = 0; i < nums.length; i++) {
			int num = nums[i];
			int mod = num % space;
			cnt[mod]++;
			min[mod] = Math.min(min[mod], num);
		}
		int ans = 0;
		for (int i = 0, size = 0; i < space; i++) {
			if (cnt[i] > size) {
				ans = min[i];
				size = cnt[i];
			} else if (cnt[i] == size && min[i] < ans) {
				ans = min[i];
			}
		}
		return ans;
	}
}
